package com.tlgen.controller;

import com.alibaba.fastjson.JSONObject;
import com.tlgen.model.App;
import com.tlgen.model.DingTalk;
import com.tlgen.model.SysUser;
import com.tlgen.service.RefreshService;
import com.tlgen.utils.FileUtils;
import com.tlgen.utils.SpringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    @Autowired
    private RefreshService refreshService;

    @GetMapping("/refreshBean")
    public String refreshBean() {
        refreshService.refreshDefinitionBean();
        return "已发布最新配置";
    }

    @GetMapping("/getBean")
    public Object getBean() {
        JSONObject jsonObject = new JSONObject();
        DingTalk dingTalk = SpringUtils.getBean(DingTalk.class);
        SysUser user = SpringUtils.getBean(SysUser.class);
        App app = SpringUtils.getBean(App.class);
        jsonObject.put("dingTalk", dingTalk);
        jsonObject.put("user", user);
        jsonObject.put("app", app);
        return jsonObject;
    }

    @PostMapping("/publish")
    public String publish(@RequestParam("content") String content) {
        FileUtils.createFile(content);
        refreshService.refreshDefinitionBean();
        return "已发布最新配置";
    }

}
